package com.spider.silence.worker;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.spider.silence.downloader.QZoneDownLoader;
import com.spider.silence.itf.output.Writer;
import com.spider.silence.output.*;
import com.spider.silence.pool.QZoneDownLoaderPool;
import com.spider.silence.task.QZoneTask;
import com.spider.silence.task.manager.CommonTaskManager;
import com.spider.silence.utils.DateUtils;
import com.spider.silence.utils.PropertiesUtil;
import org.apache.log4j.Logger;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by 夏茂轩 on 2017/1/11.
 */
public class QZoneWorker implements Runnable {
    private Logger logger = Logger.getLogger(QZoneWorker.class);

    @Override
    public void run() {
        while (true && !Thread.interrupted()) {
            QZoneDownLoaderPool downLoaderPool = QZoneDownLoaderPool.getInstance();
            QZoneDownLoader downLoader = null;
            FileWriter writer = null;
            FileWriterPool writerPool =FileWriterPool.getInstance();
            try {
                downLoader = downLoaderPool.fetchDownLoader(1000);
                if (downLoader != null) {
                    QZoneTask task = (QZoneTask) CommonTaskManager.takeTask("QZone",1000);
                    if(task==null){
                        continue;
                    }
                    String qi = task.getQi();
                    JSONArray items = downLoader.getQZItems(qi, Integer.parseInt(PropertiesUtil.getVal("QZone.downloader.crawl.item.size")));
                    writer= writerPool.fetchWriter();
                    for (int i = 0; i < items.size(); i++) {
                        String filePrefix = "QZone";
                        JSONObject item = items.getJSONObject(i);
                        JSONObject data = item.getJSONObject("data");
                        long tsp = DateUtils.dateToTimestamp(DateUtils.parseDateTime(data.getString("publish_time"))).getTime();
                        String user_id = data.getString("publish_user_id");
                        String tid = data.getString("id");
                        String fileName = filePrefix + "_" + tsp + "_" + user_id + "_" + tid;//平台名称（或者英文）_ 言论发布的时间戳 _ 发布用户的ID _ 言论的tid
                        String publishDate = DateUtils.formatDate(DateUtils.parseDate(data.getString("publish_time"))).replace("-", "-");
                        //]writer = FTPWriterPool.getInstance().fetchWriter();
                        Map<String,File> map=downLoader.getFiles();
                        Map<String,File> mapX=new HashMap<>();
                        String totalName="QQ空间" + "_speeches_" + publishDate+"_"+fileName+"_pageNo_1";
                        mapX.put(totalName,map.get(totalName));
                        writer.write("QQ空间" + "_speeches_" + publishDate, fileName, JSON.toJSONString(item, SerializerFeature.BrowserCompatible),mapX);
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
                e.printStackTrace();
            } finally {
                if(downLoader!=null){
                    QZoneDownLoaderPool.getInstance().releaseDownLoader(downLoader);
                }
                if(writer!=null){
                    writerPool.releaseWriter(writer);
                }
            }
        }
    }
}
